//
//  PersonInfoHeadCell.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/10.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "PersonInfoHeadCell.h"
#import "XXTool.h"
#import "KICropView.h"

@interface PersonInfoHeadCell ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,KICropViewDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) UIImageView *background;

@property (nonatomic,strong) UIImageView *userHead;

@property (nonatomic,strong) UIImageView *photoImg;

@end

@implementation PersonInfoHeadCell
{
    UIViewController *current;
}
-(UIImageView*)background
{
    if (_background==nil)
    {
        _background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"个人资料背景图"]];
        _background.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_background];
    }
    return _background;
}

-(UIImageView*)userHead
{
    if (_userHead==nil)
    {
        _userHead = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"默认头像"]];
        _userHead.contentMode = UIViewContentModeScaleToFill;
        _userHead.userInteractionEnabled = YES;
        _userHead.clipsToBounds = YES;
        [_userHead addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeUserHead)]];
        [self addSubview:_userHead];
    }
    return _userHead;
}

-(UIImageView*)photoImg
{
    if (_photoImg==nil)
    {
        _photoImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"照相机图标"]];
        _photoImg.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:_photoImg];
    }
    return _photoImg;
}

-(void)refreshLayout:(CGRect)frame
{
    self.background.frame = frame;
    self.userHead.frame = CGRectMake((frame.size.width-100)/2
                                 , (frame.size.height-100)/2
                                 , 100
                                 , 100);
    _userHead.layer.cornerRadius = _userHead.frame.size.width/2;
    self.photoImg.frame = CGRectMake(frame.size.width/2+20
                                 , _userHead.frame.size.height+_userHead.frame.origin.y-17
                                 , 19, 17);
}

-(void)didMoveToSuperview
{
    if([self superview]!=nil)
    {
        if(current==nil)
            current = [XXTool getViewController:self];
    }
}

-(void)setUserHeadImg:(NSString *)imgUrl compltion:(void (^)(UIImage *))block
{
    [UIImageView downloadImageWithUrl:imgUrl
                           Completion:^(UIImage *imageData) {
                               if (imageData!=nil)
                               {
                                   [_userHead setImage:imageData];
                                   if (block)
                                   {
                                       block(_userHead.image);
                                   }
                               }
                               else
                               {
                                   [_userHead setImage:[UIImage imageNamed:@"默认头像"]];
                               }
                           }];
}

-(void)setUserHeadImgWithUIImage:(UIImage *)img
{
    _userHead.image = img;
}

-(void)changeUserHead
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                               destructiveButtonTitle:NSLocalizedString(@"从相册获取", nil) otherButtonTitles:NSLocalizedString(@"拍照片", nil), nil];
    [actionSheet showInView:self];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==0)
    {//从相册
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            UIImagePickerController *picker=[[UIImagePickerController alloc] init];
            picker.delegate=self;
            picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
            picker.allowsEditing=NO;
            [current presentViewController:picker animated:YES completion:nil];
        }
        else
        {
            UIAlertView *hint=[[UIAlertView alloc] initWithTitle:nil
                                                         message:NSLocalizedString(@"图库不可用", nil)
                                                        delegate:self
                                               cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                               otherButtonTitles:nil];
            [hint show];
        }
    }
    else if(buttonIndex==1)
    {//拍照片
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker=[[UIImagePickerController alloc] init];
            picker.delegate=self;
            picker.sourceType=UIImagePickerControllerSourceTypeCamera;
            picker.allowsEditing=NO;
            [current presentViewController:picker animated:YES completion:nil];
        }
        else
        {
            UIAlertView *hint=[[UIAlertView alloc] initWithTitle:nil
                                                         message:NSLocalizedString(@"图库不可用", nil)
                                                        delegate:self
                                               cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                               otherButtonTitles:nil];
            [hint show];
        }
    }
    
}

#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage* pickImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    KICropView *crop=[[KICropView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) WithSize:CGSizeMake(250, 250)];
    [crop setDelegate:self];
    [crop setImage:pickImg];
    [current.view addSubview:crop];
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


-(void)cropView:(KICropView *)view didClickSaveImage:(UIImage *)editImage ImageRect:(CGRect)frame
{
    _userHead.image = editImage;
    if ([_delegate respondsToSelector:@selector(PersonInfoHeadCellDidSelectedHead:)])
    {
        [_delegate PersonInfoHeadCellDidSelectedHead:editImage];
    }
}

@end
